package com.fau.wmbj.server.config;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import com.fau.wmbj.server.exception.FaException;
import com.fau.wmbj.server.utils.Message;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;

/**
 * 全局异常处理
 *
 * @author jiawei
 * @version 1.0
 * @datetime 2024-02-26 16:12
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionConfig {
    /**
     * 自定义异常
     */
    @ExceptionHandler(value = FaException.class)
    public Message processException(FaException e) {
        log.error("位置:{} -> 错误信息:{}", e.getMethod(), e.getMessage());
        return Message.error(e.getCode(), e.getMessage());
    }

    /**
     * 拦截表单参数校验
     */
    @ResponseBody
    @ExceptionHandler(BindException.class)
    public Message bindExceptionHandler(BindException ex) {
        StringBuilder sb = new StringBuilder();
        BindingResult bindingResult = ex.getBindingResult();
        if (bindingResult.hasErrors()) {
            for (int i = 0; i < bindingResult.getAllErrors().size(); i++) {
                ObjectError error = bindingResult.getAllErrors().get(i);
                sb.append(i == 0 ? "" : "\n").append(error.getDefaultMessage());
            }
        }
        return Message.error(sb.toString());
    }

    @ExceptionHandler(ConstraintViolationException.class)
    @ResponseBody
    public Message handler(ConstraintViolationException ex) {
        StringBuilder sb = new StringBuilder();
        int i = 0;
        for (ConstraintViolation violation : ex.getConstraintViolations()) {
            sb.append(++i == 1 ? "" : "\n").append(violation.getMessage());
        }
        return Message.error(sb.toString());
    }

    /**
     * 请求方式不支持
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public Message httpReqMethodNotSupported(HttpRequestMethodNotSupportedException e) {
        log.error("错误信息:{}", e.getLocalizedMessage());
        return Message.error("请求方式不支持");
    }

    /**
     * 未登录异常
     */
    @ExceptionHandler(NotLoginException.class)
    public Message notLoginException(NotLoginException e) {
        return Message.error("1003", "用户未登录");
    }

    /**
     * 通用异常
     */
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(Exception.class)
    public Message exception(Exception e) {
        if (e instanceof NotPermissionException) {
            return Message.error("没有操作权限");
        }
        e.printStackTrace();
        log.error("NotPermissionException-错误信息:{}", e.getLocalizedMessage());
        return Message.error("1002",e.getMessage());
    }

}
